package blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import blog.entity.Category;
import blog.mapper.CategoryMapper;
import blog.service.CategoryService;

/**
 * 种类服务层  
 * @author 江佳洋
 *
 */
@Service
public class CategoryServiceImpl implements CategoryService{
	@Autowired
	private CategoryMapper categoryMapper;
	
	/**
	 * 根据id去article_category表中查询文章对应种类id，然后查询出种类的详细信息，返回
	 */
	@Override
	public List<Category> getArticleCategory(Integer id) {
		return categoryMapper.getArticleCategory(id);
	}
	/**
	 * 查询所有种类
	 * @return
	 */
	@Override
	public List<Category> findAllCategory() {
		List<Category> findAllCategory = categoryMapper.findAllCategory();
		for(Category cate:findAllCategory) {
			int findCategoryCountArticle = categoryMapper.findCategoryCountArticle(cate.getCategoryId());
			cate.setArticleCount(findCategoryCountArticle);
		}
		return findAllCategory;
	}
	/**
	 * 判断分类名字是否重复
	 * @return
	 */
	@Override
	public String getcategoryName(String categoryName) {
		return categoryMapper.getcategoryName(categoryName);
	}
	/**
	 * 添加分类
	 * @return
	 */
	@Override
	public int addCategory(Category category) {
		return categoryMapper.addCategory(category);
	}
	/**
	 * 删除分类
	 * @param categoryId
	 */
	public void deleteCategory(Integer categoryId) {
		categoryMapper.deleteCategory(categoryId);
	}
	/**
	 * 修改分类
	 * @param category
	 * @return
	 */
	public int updateCategoru(Category category) {
		return categoryMapper.updateCategoru(category);
	}
	/**
	 * 根据id查询分类
	 * @param id
	 * @return
	 */
	public Category selectCategoryById(Integer id) {
		return categoryMapper.selectCategoryById(id);
	}
	/**
	 * 在记录文章分类的表信息加入数据，就是article_category_ref这个表
	 * @param articleId 文章id
	 * @param categoryId  分类id
	 */
	public void addArticleCategory(Integer articleId, Integer categoryId) {
		categoryMapper.addArticleCategory(articleId,categoryId);
		
	}
	/**
	 * 删除文章时删除跟文章有关的article_category_ref表里的数据
	 * @param articleId
	 */
	public void deleteCategoryArticle(Integer articleId) {
		categoryMapper.deleteCategoryArticle(articleId);
		
	}
	/**
	 * 做菜单联动用，根据大分类id查询小分类
	 * @param id
	 * @return
	 */
	public List<Category> findSmallCategory(Integer id) {
		return categoryMapper.findSmallCategory(id);
	}

}
